package com.hntest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hntest.entity.LoginInfo;
import com.hntest.entity.Pager;
import com.hntest.entity.RealAuth;
import com.hntest.entity.UserFile;
import com.hntest.service.RealAuthService;
import com.hntest.service.UserFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller("userFileController")
public class UserFileController {

    @Autowired
    private UserFileService userFileService;

    @RequestMapping(value = "/admin/userFileSelect",method = {RequestMethod.GET,RequestMethod.POST})
    public String select(Pager pager, UserFile userFile, ModelMap modelMap){
        List<UserFile> list = userFileService.getListByPage(pager,userFile);
        modelMap.put("list",list);
        modelMap.put("pager",pager);
        modelMap.put("userFile",userFile);
        return "admin/selectUserFile";
    }


    @RequestMapping(value="/admin/userFileDoUpdate",method= {RequestMethod.GET,RequestMethod.POST},
            produces= {"application/JSON;charset=utf-8"})
    @ResponseBody
    public Object doUpdateRealAuth(UserFile userFile, HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        LoginInfo adminUser = (LoginInfo) session.getAttribute("adminUser");
        System.out.println("------------>"+adminUser);
        userFile.setAuditor(adminUser);
        System.out.println("------------>");
        int n =  userFileService.updateUserFile(userFile);
        System.out.println("------------>>>>");

        UserFile userFile1 = null;
        JSONObject jsonObject = new JSONObject();
        if (n>0){
            userFile1 = userFileService.selectById2(userFile.getId());
            jsonObject.put("success",true);
            jsonObject.put("msg",userFile1.getStateDisplay());
        }else{
            jsonObject.put("success",false);
            jsonObject.put("msg","系统异常，审核出错");
        }
        return JSON.toJSONString(jsonObject);
    }


}
